<template>
  <div id="detail">

    <detail-nav-bar @titleClick="titleClick" ref="nav"></detail-nav-bar>

    <scroll class="content" ref="scroll" @scroll="detailScroll" :probe="3">
      <template #default>
        <detail-swiper :topImages="topImages" :id="id"></detail-swiper>

        <detail-base-info :goods="goods"></detail-base-info>

        <detail-shop-info :shop="shop"></detail-shop-info>

        <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>

        <detail-param-info :param-info="paramInfo" ref="params"></detail-param-info>

        <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>

        <goods-list :goods="recommend" ref="recommend"></goods-list>
      </template>
    </scroll>

    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>

    <back-top v-show="isShow" @backClick="backClick" right="10" bottom="50"></back-top>

  </div>
</template>

<script>
  import { getDetail, getRecommend, Goods, Shop, GoodsParams } from 'network/detail.js'
  import DetailNavBar from './childComps/DetailNavBar'
  import DetailSwiper from './childComps/DetailSwiper'
  import DetailBaseInfo from './childComps/DetailBaseInfo'
  import DetailShopInfo from './childComps/DetailShopInfo'
  import DetailGoodsInfo from './childComps/DetailGoodsInfo'
  import DetailParamInfo from './childComps/DetailParamInfo'
  import DetailCommentInfo from './childComps/DetailCommentInfo'
  import DetailBottomBar from './childComps/DetailBottomBar'

  import Scroll from 'components/common/scroll/Scroll'
  import GoodsList from 'components/content/goods/GoodsList'
  import BackTop from 'components/content/backTop/BackTop'

  import { debounce } from 'common/utils'

  export default {
    name: "Detail",
    data(){
      return {
        id: null,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommend: [],
        itemImgListener: null,
        themeTopYs: [0],
        scrollIndex: 0,
        isShow: false,
      }
    },
    created(){
      this.id = parseInt(this.$route.params.id)
      this.getDetail()
      this.getRecommend()
    },
    mounted(){
      let newRefresh = debounce(this.$refs.scroll.refresh, 100)

      this.itemImgListener = () => {
        newRefresh()
      }

      this.$bus.$on('itemImageLoad', this.itemImgListener)
    },
    destroyed(){
      this.$bus.$off('itemImageLoad', this.itemImgListener)
    },
    methods: {
      getDetail(){
        getDetail().then(res => {
          if(res.data.status===0){
            var ret = res.data.message.find(t => t.id===parseInt(this.id))

            // 轮播图数据
            this.topImages = ret.itemInfo.topImages

            // 商品信息
            this.goods = new Goods(ret.itemInfo, ret.columns, ret.shopInfo.services)

            // 店铺信息
            this.shop = new Shop(ret.shopInfo)

            // 详情信息
            this.detailInfo = ret.detailInfo

            // 参数信息
            this.paramInfo = new GoodsParams(ret.itemParams)

            // 评论信息
            if(ret.rate.cRate !== 0){
              this.commentInfo = ret.rate.list[0]
            }

            // this.$nextTick(() => {
              // console.log(this.$refs.params.$el.offsetTop);
              // console.log(this.$refs.comment.$el.offsetTop);
              // console.log(this.$refs.recommend.$el.offsetTop);
              // this.themeTopYs[1] = this.$refs.params.$el.offsetTop
              // this.themeTopYs[2] = this.$refs.comment.$el.offsetTop
              // this.themeTopYs[3] = this.$refs.recommend.$el.offsetTop
              // console.log(this.themeTopYs);
            // })

          }
        })
      },
      getRecommend(){
        getRecommend().then(res => {
          if(res.data.status===0){
            this.recommend = res.data.message.list
          }
        })
      },
      imageLoad(){
        this.$refs.scroll.refresh()

        // 因为是获取到顶部的距离，包含44px导航，但是scroll滚动区域不包括导航，所以要 -44
        this.themeTopYs[1] = this.$refs.params.$el.offsetTop - 44
        this.themeTopYs[2] = this.$refs.comment.$el.offsetTop - 44
        this.themeTopYs[3] = this.$refs.recommend.$el.offsetTop - 44
      },
      titleClick(index){
        this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 500)
      },
      detailScroll(position){
        // 获取滚动的Y值
        let scrollY = -position.y

        // console.log(scrollY);

        if(scrollY < this.themeTopYs[1] && this.scrollIndex!==0){
          this.scrollIndex = 0
        }else if(scrollY >= this.themeTopYs[1] && scrollY < this.themeTopYs[2] && this.scrollIndex!==1){
          this.scrollIndex = 1
        }else if(scrollY >= this.themeTopYs[2] && scrollY < this.themeTopYs[3] && this.scrollIndex!==2){
          this.scrollIndex = 2
        }else if(scrollY >= this.themeTopYs[3] && this.scrollIndex!==3){
          this.scrollIndex = 3
        }

        // 判断BackTop是否显示
        this.isShow = scrollY > 600
      },
      backClick(){
        // 直接通过 ref 获取组件，调用组件内部方法
        this.$refs.scroll.backTop(0, 0, 300)
      },
      addToCart(){
        // 1.获取购物车需要展示的信息
        const obj = {
          image: this.topImages[0],
          title: this.goods.title,
          desc: this.goods.desc,
          price: this.goods.realPrice,
          count: 1,
          id: this.id
        }
        // 2.将商品添加到购物车
        this.$store.dispatch('addCart', obj).then(res => {
          this.$toast.show(res)
        }, err => {
          console.log(err);
        })

      }
    },
    watch: {
      scrollIndex(newVal){
        console.log(newVal, 123);
        this.$refs.nav.currentIndex = newVal
      }
    },
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailBottomBar,
      Scroll,
      GoodsList,
      BackTop,
    }
  }
</script>

<style lang="less" scoped>
#detail{
  height: 100vh;
  position: relative;
  z-index: 90;
  background-color: #fff;
  .content{
    height: calc(100% - 44px - 49px);
  }
}
</style>
